<!--
 * @Author: your name
 * @Date: 2021-12-17 13:43:26
 * @LastEditTime: 2021-12-17 15:25:05
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \day9\my-app\src\views\Login.vue
-->
/<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="FormData"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="FormData.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          v-model="FormData.pwd"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";

// 引入接口
import {login} from "../api/api"

// 跳转页面   useRouter  useRoute
import {useRouter} from "vue-router"

export default defineComponent({
  setup() {
    // 绑定ref
    const ruleForm = ref();

    // 跳转页面
    const router=useRouter()

    const FormData = reactive({
      name: "",
      pwd: "",
    });

    // 点击登录
    const submitForm = () => {
        //  ruleForm.value  找到绑定ref的dom元素
        // valid 布尔类型
      ruleForm.value.validate(async(valid:boolean) => {
        if (valid) {
        //   alert("submit!");
        //   跳接口
        // 异步接口  async  await 
        // res 接口返回的数据类型  不确定 写any
        let res:any=await login(FormData);
        console.log(res);
        if(res.status==200){
            //跳转页面
            router.push({
                path:"/list"
            })
        }
        
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    }
    // 校验规则
    const rules = reactive({
      name: [
        {
          required: true,
          message: "Please input Activity name",
          trigger: "blur",
        },
      ],
      pwd: [
        {
          required: true,
          message: "Please input Activity name",
          trigger: "blur",
        },
        {
          min: 6,
          max: 10,
          message: "Length should be 6 to 10",
          trigger: "blur",
        },
      ],
    });

    return {
      ruleForm,
      FormData,
      submitForm,
      rules,
    };
  },
});
</script>

<style>
</style>